package org.qq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.qq.clientthread.ClientLoginThread;
import org.qq.util.ClientAllData;
import org.qq.util.Util;

/**
 * 
 * project_name:QQ
 * package_name:org.qq.register
 * file_name:PrivateChatLabel.java
 * @author cui&&Link
 * create_date:2015年8月28日 上午9:41:22
 * modified_date:
 * description:私聊窗口
 *
 */
public class PrivateChatRoom extends JFrame{
	private JComboBox faceBox;							//表情下拉框
	private ImageIcon tu,tu2;							//图片
	private JButton modifyButton;						//修改用户资料按钮
	private JButton groupButton,PrivateChatButton;		//群聊,私聊按钮
	private JMenuBar menuBar;							//菜单栏
	private JButton sendButton;							//发消息按钮
	private JLabel faceLabel,faceShowLabel;				//表情标签,马赛克标签
	private String[] str = {"*^*"};						
	private JTextField sendField;						//消息文本框
	private JTextArea textArea;							//消息区域
	private Socket s;									//套接字
	private JScrollPane jScrollPane;					//消息区域下拉框
	private ClientLoginThread clt;						//用户端接收发送消息线程
	
	
	public PrivateChatRoom(){			//无參构造方法
		super();
	}
	
	public Socket getS() {
		return s;
	}
	public void setS(Socket s) {
		this.s = s;
	}
	
	public JTextArea getTextArea() {
		return textArea;
	}

	public void setTextArea(JTextArea textArea) {
		this.textArea = textArea;
	}

	public PrivateChatRoom(Socket s){
		this.s = s;
		setBackground(Color.GRAY);
		getContentPane().setBackground(Color.LIGHT_GRAY);
		this.setBounds(0, 0, 800, 600);
		this.setTitle("私聊");
		Util.screen(this);//调用Util包里面居中方法
		//this.setDefaultCloseOperation(3);
		getContentPane().setLayout(null);
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				PrivateChatRoom.this.setVisible(false);
				ClientAllData.privateChatRooms.remove(PrivateChatRoom.this);
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		tu = new ImageIcon(PrivateChatRoom.class.getResource("/image/J0099191.JPG"));					 		// 图片
		tu2 = new ImageIcon(PrivateChatRoom.class.getResource("/image/target_florian.jpg"));					 // 图片
		this.setIconImage(tu2.getImage());
		this.setResizable(false); 														// 不可改变大小
		getContentPane().setLayout(null); 												// 自定义
		
		faceBox = new JComboBox(str);														//表情框
		faceBox.setBackground(Color.ORANGE);
		faceBox.setBounds(42, 418,490,33);
		faceBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sendField.setText(sendField.getText()+faceBox.getSelectedItem().toString());
			}
		});
		getContentPane().add(faceBox);
		
		clt = new ClientLoginThread(s);
		/**
		 * 发消息按钮
		 */
		sendButton = new JButton("发送");
		sendButton.setBackground(Color.ORANGE);
		sendButton.setMargin(new Insets(0, 0, 0, 0));  			//无间距
		sendButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		sendButton.setForeground(Color.BLACK);
		sendButton.setBounds(539, 440, 67, 52);
		getContentPane().add(sendButton);
		sendButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!sendField.getText().equals("")){
					String sendMessage = sendField.getText()+PrivateChatRoom.this.getTitle().substring(PrivateChatRoom.this.getTitle().length()-9, PrivateChatRoom.this.getTitle().length()-1)
						+"----privateChat";
					clt.sendMessageToServer(sendMessage);
					textArea.append(new SimpleDateFormat("HH:mm:ss").format(new Date())+"  "+"自己"+"  say:"+"\n"+sendField.getText()+"\n");
					sendField.setText("");
					for(int i = 0;i<ClientAllData.privateChatRooms.size();i++){
						if(ClientAllData.privateChatRooms.get(i).equals(PrivateChatRoom.this)){
							return;
						}else{
							continue;
						}
					}
					ClientAllData.privateChatRooms.add(PrivateChatRoom.this);
				}
			}
		});
		
		faceShowLabel = new JLabel(tu);								//右上角的图片
		faceShowLabel.setBounds(611, 20, 173, 200);
		getContentPane().add(faceShowLabel);
		
		faceLabel = new JLabel("表情");
		faceLabel.setForeground(Color.blue);
		faceLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		faceLabel.setBounds(0, 417, 51, 33);
		getContentPane().add(faceLabel);
		
		textArea = new JTextArea();
		textArea.setBounds(0, 20, 584, 362);
		textArea.setEditable(false);
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		
		jScrollPane = new JScrollPane(textArea);
		jScrollPane.setBounds(0, 20, 584, 362);
		getContentPane().add(jScrollPane);
		
		sendField = new JTextField();
		sendField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		sendField.setBounds(10, 461, 516, 46);
		getContentPane().add(sendField);
		sendField.setColumns(10);
		sendField.addKeyListener(new KeyListener() {//回车事件
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					if(!sendField.getText().equals("")){
						String sendMessage = sendField.getText()+PrivateChatRoom.this.getTitle().substring(PrivateChatRoom.this.getTitle().length()-9, PrivateChatRoom.this.getTitle().length()-1)
							+"----privateChat";
						clt.sendMessageToServer(sendMessage);
						textArea.append(new SimpleDateFormat("HH:mm:ss").format(new Date())+"  "+"自己"+"  say:"+"\n"+sendField.getText()+"\n");
						sendField.setText("");
						for(int i = 0;i<ClientAllData.privateChatRooms.size();i++){
							if(ClientAllData.privateChatRooms.get(i).equals(PrivateChatRoom.this)){
								return;
							}else{
								continue;
							}
						}
						ClientAllData.privateChatRooms.add(PrivateChatRoom.this);
					}
				}
			}
		});
		
		menuBar = new JMenuBar();
		menuBar.setBackground(Color.GRAY);
		setJMenuBar(menuBar);
		
		modifyButton = new JButton("用户修改");								//点击切换到用户修改界面
		modifyButton.setForeground(Color.BLACK);
		modifyButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		modifyButton.setBackground(Color.LIGHT_GRAY);
		menuBar.add(modifyButton);
		
		groupButton = new JButton("群聊");					//点击切换到群聊界面
		groupButton.setForeground(Color.BLACK);
		groupButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		groupButton.setBackground(Color.LIGHT_GRAY);
		menuBar.add(groupButton);
		
		PrivateChatButton = new JButton("私聊");		//点击提示在私聊界面
		PrivateChatButton.setForeground(Color.BLACK);
		PrivateChatButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		PrivateChatButton.setBackground(Color.PINK);
		menuBar.add(PrivateChatButton);
	
	}

	/**
	 * 重写equals方法(根据title来判断是否相同)
	 */
	@Override
	public boolean equals(Object arg0) {
		if(arg0 instanceof PrivateChatRoom){
			PrivateChatRoom pcr = (PrivateChatRoom)arg0;
			if(pcr.getTitle().toString().equals(this.getTitle().toString())){
				return true;
			}
		}
		return false;
	}
	
	
}

